/*
 *    Copyright (C) 2007-2009 Aike J Sommer
 * 
 * 
 *    This program is free software: you can
 *    redistribute it and/or modify it under the terms of the GNU 
 *    General Public License as published by the Free Software 
 *    Foundation, either version 3 of the License, or (at your 
 *    option) any later version.
 *
 *    This program is distributed in the hope
 *    that it will be useful, but WITHOUT ANY WARRANTY; without even 
 *    the implied warranty of MERCHANTABILITY or FITNESS FOR A 
 *    PARTICULAR PURPOSE.  See the GNU General Public License for 
 *    more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see
 *    <http://www.gnu.org/licenses/>.
 * 
 * 
 *    You can reach the author and get more information about this 
 *    project at: http://aikesommer.name/ or
 *    http://code.google.com/p/viewncontrol
 */

package name.aikesommer.viewncontrol.web;

import java.io.Serializable;
import java.util.Comparator;

/**
 *
 * @author Aike J Sommer <as@as-media.com>
 */
public class Pair<T1, T2> implements Serializable {
    
    private T1 first;
    private T2 second;
    
    
    /** Creates a new instance of Pair */
    public Pair(T1 first, T2 second) {
        this.first = first;
        this.second = second;
    }
    
    public Pair() {
        
    }
    
    
    public T1 getFirst() {
        return first;
    }
    
    public void setFirst(T1 first) {
        this.first = first;
    }
    
    public T2 getSecond() {
        return second;
    }
    
    public void setSecond(T2 second) {
        this.second = second;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Pair<T1, T2> other =
                (Pair<T1, T2>) obj;
        if (this.first != other.first && (this.first == null || !this.first.equals(other.first))) {
            return false;
        }
        if (this.second != other.second && (this.second == null || !this.second.equals(other.second))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 37 * hash + (this.first != null ? this.first.hashCode() : 0);
        hash = 37 * hash + (this.second != null ? this.second.hashCode() : 0);
        return hash;
    }
    
    
    public static class FirstComparator<T1 extends Pair<Comparable, Object>> implements Comparator<T1> {
        
        public int compare(T1 o1, T1 o2) {
            if (o1 == null
                    || o2 == null
//                    || ! (o1 instanceof Pair)
//                    || ! (o2 instanceof Pair)
//                    || ! (((Pair)o1).getFirst() instanceof Comparable)
//                    || ! (((Pair)o2).getFirst() instanceof Comparable)
                    )
                return 0;
            
            return ((Comparable)((Pair)o1).getFirst()).compareTo(((Pair)o2).getFirst());
        }
        
    }
    
}
